package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public void saveDishWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.insertDish(dish);
//      获取刚刚生成的菜品主键ID
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();

        flavors.forEach(
                dishFlavor -> dishFlavor.setDishId(dishId)
                );
        dishFlavorMapper.insertBachFlavor(flavors);
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
//      获取刚刚生成的菜品主键ID
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null || flavors.size() > 0){
            dishFlavorMapper.deleteFlavorByDishId(dishId);
        }
        flavors.forEach(
                dishFlavor -> dishFlavor.setDishId(dishId)
        );
        dishFlavorMapper.insertBachFlavor(flavors);
    }

    @Override
    public DishVO getDishInfoById(Long id) {
         Dish dish = dishMapper.get(id);
         DishVO dishVO = new DishVO();
         BeanUtils.copyProperties(dish,dishVO);
         List<DishFlavor> flavors = dishFlavorMapper.loadFlavorsByDishId(id);
         dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<Dish> dishPage = dishMapper.QueryDishPage(dishPageQueryDTO);
        return new PageResult(dishPage.getTotal(),dishPage.getResult());
    }

    @Override
    public void bachDelete(String[] ids) {
        dishMapper.bachDelete(ids);
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public List<Dish> getDishByCategoryId(Long categoryId) {
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        List<Dish> list = dishMapper.list(dish);
        return list;
    }
}
